#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <glib.h>
#include <libxml/xmlreader.h>
#include <python2.6/Python.h>
#define BUF_SIZ 265
struct {
	gchar szID[BUF_SIZ];
	gchar szTitle[BUF_SIZ];
	gchar szArtist[BUF_SIZ];
	gchar szFileName[BUF_SIZ];
}g_argv;

gchar * SearchLyric(PyObject* pmod, gchar * lpszArtist, gchar * lpszTitle)
{
	PyObject* pFunSearchLyric = PyObject_GetAttrString(pmod, "SearchLyric");
	if(!pFunSearchLyric)
	{
		printf("error when get attrstring SearchLyric\n");
		return;
	}
	PyObject * pArgv = Py_BuildValue("(ss)", lpszArtist, lpszTitle);
	if (!pArgv) {
		printf("error when Py_BuildValue\n");
		return;
	}
	PyObject *pstr = PyEval_CallObject(pFunSearchLyric, pArgv);
	if (pstr)
	{
		char *lpszList = NULL;
		PyArg_Parse(pstr, "s", &lpszList);
		return lpszList;
	}
	return NULL;
}
gchar* DownLoadLyric(PyObject* pmod, gchar* lpszID, gchar * lpszArtist, gchar * lpszTitle)
{
	PyObject* pFunDownLoad = PyObject_GetAttrString(pmod, "DownLoadLyric");
	if(!pFunDownLoad)
	{
		printf("error when get attrstring pFunDownLoad\n");
		return;
	}
	PyObject * pArgv = Py_BuildValue("(sss)", lpszID, lpszArtist, lpszTitle);
	PyObject *pstr = PyEval_CallObject(pFunDownLoad, pArgv);
	gchar *lpszContent = NULL;
	if (pstr)
	{
		PyArg_Parse(pstr, "s", &lpszContent);
	}
	return lpszContent;
}
gchar * ParseString(gchar* lpszStr)
{
	if (!lpszStr) return NULL;
	if (lpszStr[0] == '"')
	{
		int iLen = strlen(lpszStr);
		lpszStr[iLen-1] = 0;
		return lpszStr+1;
	}
	return lpszStr;
}
void Help()
{
	printf("argvs: -t:title -a:artist -i:id -o:fileName\n");
}
gboolean ParseArgv(gchar* lpszArgv)
{
	if (!lpszArgv || 
		strlen(lpszArgv) < 4 ||
		lpszArgv[0] != '-')
	{
		return FALSE;
	}
	gchar* pValue = strstr(lpszArgv, ":");
	gchar* pName  = lpszArgv;
	if(pValue)
	{
		*pValue = '\0';
		pValue++;
	}
	pValue = ParseString(pValue);
	if (strcmp(pName, "-i") == 0)
	{
		strncpy(g_argv.szID, pValue, BUF_SIZ);
		//printf("g_argv.szID=%s\n", pValue);
	}
	else if (strcmp(pName, "-t") == 0)
	{
		strncpy(g_argv.szTitle, pValue, BUF_SIZ);
		//printf("g_argv.szTitle=%s\n", g_argv.szTitle);
	}
	else if (strcmp(pName, "-a") == 0)
	{
		strncpy(g_argv.szArtist, pValue, BUF_SIZ);
		//printf("g_argv.szArtist=%s\n", pValue);
	}
	else if (strcmp(pName, "-o") == 0)
	{
		strncpy(g_argv.szFileName, pValue, BUF_SIZ);
		//printf("g_argv.szFileName=%s\n", pValue);
	}
	else if (strcmp(pName, "-h") == 0 || strcmp(pName, "--help") == 0)
	{
		Help();
		exit(0);
	}
	else
	{
		printf("Unkown argv: %s\n", lpszArgv);
		Help();
		exit(1);
	}
	return TRUE;
}

int main(int argc, char * argv[])
{
	int i;
	memset(&g_argv, 0, sizeof(g_argv));
	for (i = 1; i < argc; i++)
	{
		ParseArgv(argv[i]);
	}
	if (strlen(g_argv.szTitle) <= 0)
	{
		printf("歌曲名是必须指定的.\n");
		Help();
		return 0;
	}
	Py_Initialize();
	PyRun_SimpleString("import sys, os\nsys.path.append(os.path.abspath('/usr/share/ShowLyric'))\nsys.path.append(os.path.abspath('./'))\n");
	PyObject* pmod = PyImport_ImportModule("BDDownLoadLyric");
	if(!pmod)
	{
		printf("Error when import BDDownLoadLyric\n");
		return;
	}
	gchar * lpszReturn = NULL;
	if (strlen(g_argv.szID) > 0)
	{
		lpszReturn = DownLoadLyric(pmod, g_argv.szID, g_argv.szArtist, g_argv.szTitle);
	}
	else
	{
		lpszReturn = SearchLyric(pmod, g_argv.szArtist, g_argv.szTitle);
	}
	if (strlen(g_argv.szFileName) > 0)
	{
		gchar szPath[512] = {0};
		strcpy(szPath, g_argv.szFileName);
		int i = 0;
		for (i = strlen(szPath); i >= 0; i--) {
			if (szPath[i] == '\\' || szPath[i] == '/') {
				szPath[i] = 0;
				break;
			}
		}
		
		if (strlen(szPath) > 0 && access(szPath, F_OK) == -1) {
			printf("Make dir (%s)\n", szPath);
			mkdir(szPath, 0755);
		}
		FILE * pFile = fopen(g_argv.szFileName, "w+");
		if (!pFile)
		{
			printf("open file %s failed!\n", g_argv.szFileName);
			return -1;
		}
		fwrite(lpszReturn, 1, strlen(lpszReturn), pFile);
		fclose(pFile);
		pFile = NULL;
	}
	else
	{
		printf("%s\n", lpszReturn);
	}
	Py_Finalize();
	return 0;
}
